const DB = require('../../utils/db');

module.exports = {
	list: async (userId, keywords, lable, type, pageNum, pageSize) => {
		// 查询登录用户的点赞记录

		let [likeList] = await DB.query(`select * from t_likes where userId=?`, [userId]);

		let [allList] = await DB.query(`select * from t_acticle where title like '%${keywords}%' and if(${type}=0,1,type=${type})`);
		let arr = [(pageNum - 1) * pageSize, pageSize];

		//  3个表查询

		let newSql = ` select d.id,d.title,d.isLike, d.likes,d.visitors, d.abstract,e.comments,d.visitors, d.url,d.fullUrl, d.is_hot isHot, d.is_top isTop,
                  d.is_recommend isRecommend, d.state, d.type,date_format(d.create_time,'%Y-%m-%d') createTime, date_format(d.update_time,'%Y-%m-%d') updateTime, 
                  d.labelNames, c.name typeName 
                  FROM t_acticle_type c 
                  JOIN (select a.*,GROUP_CONCAT(b.name) labelNames 
                  FROM t_acticle a 
                  JOIN t_label b
                  ON FIND_IN_SET(b.id,a.label) GROUP BY a.id) as d
                  ON c.id = d.type 
                 left join (select acticleId ,count(1) as comments from t_comment group by acticleId) as e 
                  on d.id = e.acticleId
                  where d.title like '%${keywords}%' and if(${type}=0,1,type=${type}) and d.labelNames like '%${lable}%' order by d.is_top asc, d.update_time desc limit ?,? `;
		// 文章列表
		let [list] = await DB.query(newSql, arr);

		// 查询用户是否点赞
		if (likeList.length) {
			list.forEach((item) => {
				likeList.forEach((val) => {
					if (item.id == val.acticleId) {
						item.isLike = val.isLike;
					}
				});
			});
		} else {
			list.forEach((item) => (item.isLike = 2));
		}
		return { data: list, total: allList.length };
	},
	detail: async (id) => {
		await DB.query('update t_acticle set visitors=visitors+1 where id=?', [id]);
		let sql = `select d.id,d.title,d.comments,d.likes,d.visitors, d.content, d.abstract, d.url, d.fullUrl, d.is_hot isHot, d.is_top isTop, d.is_recommend isRecommend, d.state, d.type,
               date_format(d.create_time,'%Y-%m-%d ') createTime, date_format(d.update_time,'%Y-%m-%d ') updateTime, d.labelNames,
               c.name typeName from t_acticle_type c join
               (select a.*,GROUP_CONCAT(b.name) labelNames from t_acticle a join t_label b on FIND_IN_SET(b.id,a.label) GROUP BY a.id) as d
                ON c.id = d.type where d.id=?`;
		let [list] = await DB.query(sql, [id]);
		return list[0];
	},
	listByTypeId: async (userId, id, pageNum, pageSize) => {
		// 查询登录用户的点赞记录

		let [likeList] = await DB.query(`select * from t_likes where userId=?`, [userId]);

		let [allList] = await DB.query(`select * from t_acticle where type=${id}`);

		let arr = [id, (pageNum - 1) * pageSize, pageSize];

		let sql = `select d.id,d.title,d.isLike,d.likes, d.visitors, d.content, d.abstract, d.url,d.fullUrl, d.is_hot isHot, d.is_top isTop, d.is_recommend isRecommend, d.state, d.type,
               date_format(d.create_time,'%Y-%m-%d') createTime, date_format(d.update_time,'%Y-%m-%d') updateTime, d.labelNames,
               c.name typeName from t_acticle_type c join
               (select a.*,GROUP_CONCAT(b.name) labelNames from t_acticle a join t_label b on FIND_IN_SET(b.id,a.label) GROUP BY a.id) as d
                ON c.id = d.type where d.type=? limit ?,?`;

		let [list] = await DB.query(sql, arr);

		// 查询用户是否点赞
		if (likeList.length) {
			list.forEach((item) => {
				likeList.forEach((val) => {
					if (item.id == val.acticleId) {
						item.isLike = val.isLike;
					}
				});
			});
		} else {
			list.forEach((item) => (item.isLike = 2));
		}
		return { data: list, total: allList.length };
	},
	// 点赞
	giveLike: async (userId, acticleId) => {
		console.log(userId, '000');

		let [list] = await DB.query('select * from t_likes where userId=? and acticleId=?', [userId, acticleId]);
		// 如果有记录，则取消点赞，删除记录。否则添加记录 isLike =2 未点赞 1 已点赞
		if (list.length) {
			await DB.query('delete from t_likes  where acticleId=? and userId=?', [acticleId, userId]);
			await DB.query('update t_acticle set likes=likes-1 where id=?', [acticleId]);
		} else {
			await DB.query('insert into t_likes (userId,acticleId,isLike) values(?,?,1)', [userId, acticleId]);
			await DB.query('update t_acticle set likes=likes+1 where id=?', [acticleId]);
		}
		// 返回当前修改文章的点赞
		let [likeList] = await DB.query('select *  from t_likes where userId=? and acticleId=?', [userId, acticleId]);
		let [acticleList] = await DB.query('select likes,id from t_acticle where id=?', [acticleId]);

		if (!likeList.length) {
			return { isLike: 2, likes: acticleList[0].likes, id: acticleId };
		} else {
			return { isLike: 1, likes: acticleList[0].likes, id: acticleId };
		}
	}
};
